package com.example.aop.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

// 定义切面

/**
 * 动态生成代理类对象
 */
@Aspect
@Component
public class AopAspect {
    // 定义切入点
    @Pointcut("execution(* com.example.aop.proxy.OrderService.*(..))")
    public void aPointCut(){}

    // 前置通知
    @Before("aPointCut()")
    public void doBefore(JoinPoint joinPoint){
        System.out.println("Before Aop 方法开始 " + joinPoint.getSignature().getName());
    }
    // 环绕通知
    @Around("aPointCut()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
        long startTime = System.currentTimeMillis();
        System.out.println("Around Aop 方法开始 " + pjp.getSignature().getName());
        Object obj = pjp.proceed();//实现真实方法
        long endTime = System.currentTimeMillis();
        System.out.println("Around Aop 方法结束 " + pjp.getSignature().getName());
        System.out.println("【AOP】方法结束，耗时：" + (endTime - startTime) + "ms");
        return obj;
    }

}
